<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/img/touxiang.jpg" />
      </div>
      <el-form ref="loginFormRef" :rules="loginFormRules" :model="loginForm">
        <h1>{{ title }}</h1>
        <el-form-item prop="username">
          <el-input
            type="text"
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="loginForm.password"
            placeholder="请输入密码"
            prefix-icon="iconfont icon-3702mima"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login" plain>登录</el-button>
          <el-button type="info" @click="resetLoginForm" plain>重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      title: '登 录',
      loginForm: { username: 'admin', password: '123456' },
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在3-8个字符', trigger: 'blur' }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            transform: value => value,
            trigger: 'blur'
          }
          // {
          //   type: 'string',
          //   message: '请输入不包含空格的字符',
          //   trigger: 'blur',
          //   transform(value) {
          //     if (value && value.indexOf(' ') === -1) {
          //       return value
          //     } else {
          //       return false
          //     }
          //   }
          // },
          // {
          //   trigger: 'blur',
          //   validator: (rule, value, callback) => {
          //     var passwordreg = /^(?=.*[a-zA-Z])(?=.*[0-9])[A-Za-z0-9]{8,18}$/
          //     if (!passwordreg.test(value)) {
          //       callback(new Error('密码必须字母和数字,且长度在6-18位之间'))
          //     } else {
          //       callback()
          //     }
          //   }
          // }
        ]
      }
    }
  },
  methods: {
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields()
    },
    login() {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) {
          return
        }
        await this.$http.post('login', this.loginForm).then(res => {
          if (res.data.meta.status !== 200) {
            return this.$message.error('登录失败')
          } else {
            this.$message.success('登录成功')
            window.sessionStorage.setItem('token', res.data.data.token)
            this.$router.push('/home')
          }
        })
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  height: 100%;
  background: #e0e0e0;
}
.login_box {
  width: 300px;
  position: absolute;
  top: 50%;
  left: 50%;
  padding: 50px 30px;
  border-radius: 50px;
  background: #e0e0e0;
  transform: translate(-50%, -50%);
  box-shadow: 20px 20px 60px #bebebe, -20px -20px 60px #ffffff;
}
h1 {
  text-align: center;
}
.btns {
  display: flex;
  justify-content: space-between;
}
.avatar_box {
  width: 120px;
  height: 120px;
  margin: 30px auto;
  margin-top: 0px;
  padding: 5px;
  background: #e0e0e0;
  border: 1px solid black;
  border-radius: 50%;
  overflow: hidden;
  box-shadow: 20px 20px 60px #bebebe, -20px -20px 60px #ffffff;
  img {
    border-radius: 50%;
    width: 100%;
    height: 100%;
  }
}
</style>
